package com.ticket.sass.provider.config;

import org.hibernate.validator.HibernateValidator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.validation.Validation;
import javax.validation.Validator;

/**
 * @author ywb
 * @date 2023-02-22 18:37
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    
    /**
     * 校验快速失败
     */
    @Bean
    public Validator validator() {
        return Validation.byProvider(HibernateValidator.class)
            .configure()
            .failFast(true)
            .buildValidatorFactory()
            .getValidator();
    }
    
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
            .allowedOriginPatterns("*") // 允许所有域名，可以根据实际情况修改为明确的域名或使用通配符
            .allowCredentials(true)
            .allowedMethods("GET", "POST", "PUT", "DELETE");
    }

}

